<?php

namespace app\admin\controller;

class Cate extends Base
{
    //栏目列表
    public function catelist()
    {
        $catelist = model('common/Cate') -> order(['sort']) -> paginate(5);
        //定义一个模板数据变量
        $viewData = [
            'catelist' => $catelist
        ];
        $this -> assign($viewData);
        return view();
    }

    //栏目添加
    public function cateadd()
    {
        if (request()->isAjax()){
            $data = [
                'catename' => input('post.catename'),
                'sort' => input('post.catesort')
            ];
            $result = model('common/Cate') -> cateadd($data);
            if ($result == 1){
                $this -> success('栏目添加成功！','admin/cate/catelist');
            } else {
                $this -> error($result);
            }
        }
        return view();
    }

    //栏目排序
    public function catesort()
    {
        $data = [
            'id' => input('post.id'),
            'sort' => input('post.sort'),
        ];
        $result = model('common/Cate') -> catesort($data);
        if ($result == 1){
            $this -> success('排序成功！','admin/cate/catelist');
        } else {
            $this -> error($result);
        }
    }

    //栏目编辑
    public function cateedit()
    {
        if (request()->isAjax()){
            $data = [
                'id' => input('post.id'),
                'catename' => input('post.catename')
            ];
            $result = model('common/Cate') -> cateedit($data);
            if ($result == 1){
                $this -> success('栏目编辑成功！','admin/cate/catelist');
            } else {
                $this -> error($result);
            }
        }
        $cateInfo = model('common/Cate') -> find(input('id'));
        $viewData = [
            'cateInfo' => $cateInfo
        ];
        $this -> assign($viewData);
        return view();
    }

    //栏目删除
    public function catedel()
    {
        $cateInfo = model('common/Cate') -> find(input('id'));
        //数据软删除
        $result = $cateInfo -> delete();
        if ($result){
            $this -> success('栏目删除成功！','admin/cate/catelist');
        } else {
            $this -> error('栏目删除失败！');
        }

    }

}
